package com.etnoe.gis.controller;

import com.etnoe.gis.common.domain.Gps;
import com.etnoe.gis.common.dto.ConvertsDTO;
import com.etnoe.gis.common.dto.CoordDistanceDTO;
import com.etnoe.gis.common.vo.ResponseVO;
import com.etnoe.gis.common.vo.Result;
import com.etnoe.gis.service.GisServiceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author 张任豪
 */
@Slf4j
@AllArgsConstructor
@RestController
@Api(tags = "GIS服务", value = "GisServiceController")
public class GisServiceController {

    private GisServiceService gisServiceService;

    @ApiOperation(value = "经纬度转换")
    @GetMapping("/gis/lonLatConvert")
    public String lonLatConvert(
            @ApiParam("经度") @RequestParam("fLon") Double fLon,
            @ApiParam("纬度") @RequestParam("fLat") Double fLat,
            @ApiParam("源经纬度,1：google卫星坐标gps84、2：google地图gcj02、3：百度0911、4：百度mc")
            @RequestParam("srcLonLat") Integer srcLonLat,
            @ApiParam("目标经纬度,1：google卫星坐标gps84、2：google地图gcj02、3：百度0911、4：百度mc")
            @RequestParam("tgtLonLat") Integer tgtLonLat,
            @ApiParam("返回数据:1表示返回经度，2表示返回纬度，其他返回经纬度") @RequestParam("returnType") Integer returnType
    ) {
        return gisServiceService.lonLatConvert(fLat, fLon, srcLonLat, tgtLonLat, returnType);
    }

    @ApiOperation(value = "坐标是否在多边形面中(点面求交)")
    @GetMapping("/gis/pointInPoly")
    public ResponseVO<String> pointInPoly(
            @ApiParam("经度") @RequestParam("fLon") Double fLon,
            @ApiParam("纬度") @RequestParam("fLat") Double fLat,
            @ApiParam("经纬度集合") @RequestBody List<Gps> gpsList
    ) {
        return gisServiceService.pointInPoly(fLat, fLon, gpsList);
    }

    @ApiOperation(value = "计算两个经纬度之间的距离")
    @GetMapping("/gis/distance")
    public ResponseVO<Double> distance(
            @ApiParam("经度1") @RequestParam("lon1") Double lon1,
            @ApiParam("纬度1") @RequestParam("lat1") Double lat1,
            @ApiParam("经度2") @RequestParam("lon2") Double lon2,
            @ApiParam("纬度2") @RequestParam("lat2") Double lat2
    ) {
        return gisServiceService.distance(lon1, lat1, lon2, lat2);
    }

    @ApiOperation(value = "经纬度距离计算模板下载")
    @GetMapping("/gis/distanceTemplate")
    public void distanceTemplate(HttpServletResponse response) {
        gisServiceService.distanceTemplate(response);
    }


    @ApiOperation(value = "批量测算经纬度距离(模板)")
    @PostMapping("/gis/distanceCalculate")
    public void distanceCalculate(@ApiParam("上传测算距离模板文件") @RequestPart MultipartFile file, HttpServletResponse response) {

        try {
            gisServiceService.distanceCalculate(file, response);
        } catch (Exception e) {
            log.error("解析错误", e);
        }
    }

    @ApiOperation(value = "批量测算经纬度距离")
    @PostMapping("/gis/distanceCalculates")
    public Result distanceCalculates(@ApiParam("经纬度坐标集合") @RequestBody CoordDistanceDTO distances) {
        return gisServiceService.distanceCalculates(distances.getDistances());
    }


    @ApiOperation(value = "经纬度转换模板下载")
    @GetMapping("/gis/lonLatTemplate")
    public void lonLatTemplate(HttpServletResponse response) {
        gisServiceService.lonLatTemplate(response);
    }

    @ApiOperation(value = "批量经纬度转换(模板)")
    @PostMapping("/gis/batchLonLatConvert")
    public void batchLonLat(@ApiParam("上传经纬度模板文件") @RequestPart MultipartFile file, HttpServletResponse response) {
        try {
            gisServiceService.batchLonLat(file, response);
        } catch (Exception e) {
            log.error("解析错误", e);
        }

    }

    @ApiOperation(value = "批量经纬度坐标转换")
    @PostMapping("/gis/lonLatConverts")
    public Result lonLatConverts(@ApiParam("经纬度坐标转换集合") @RequestBody ConvertsDTO converts) {
        return gisServiceService.lonLatConverts(converts.getConverts());
    }
}
